$(()=>{
    if(sessionStorage.getItem("msg_data")==null){
        alert("您的登录信息已过期请重新登陆");
        window.location.href="http://172.18.80.9:8087/admin/login/login"
    }
    var msg_data =JSON.parse(sessionStorage.getItem("msg_data"));
    console.log(msg_data.id)
    console.log(msg_data.token);
    var tid =location.search.split("add=")[1];
    if(tid!=undefined) {
        //请选择项目
        $.ajax({
            type: "POST",
            dataType:"json",
            data:{uid:msg_data.id, token:msg_data.token,},
            url: "http://172.18.80.9:8087/admin/research/pName",
            success: function (data) {
                var html=`<option value="">请选择项目</option>`;
                for(var d of data.data){
                    html+=`<option value="${d.project_id+"?"+d.project_name}">${d.project_name}</option>`
                }
                $("#project").html(html);
            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    }
    //编辑
    var loc=location.href;
    var n1=loc.length;//地址的总长度
    var n2=loc.indexOf("id=");//取得=号的位置
    if(n2!=-1){
        var tid=decodeURI(loc.substr(n2+3, n1-n2));
        //请选择项目
        $.ajax({
            type: "POST",
            dataType:"json",
            data:{uid:msg_data.id, token:msg_data.token,},
            url: "http://172.18.80.9:8087/admin/research/pname",
            success: function (data) {
                var html=`<option value="">请选择项目</option>`;
                for(var d of data.data){
                    html+=`<option value="${d.project_id+"?"+d.project_name}">${d.project_name}</option>`
                }
                $("#project").html(html);
                //详情
                $.ajax({
                    type:"POST",
                    data:{id:tid,uid:msg_data.id,token:msg_data.token},
                    dataType:"json",
                    url:"http://172.18.80.9:8087/admin/research/dpPilot",
                    success:function(data){
                        console.log(data.data);
                        for(d of data.data){
                            //项目
                            $("#project>option:first-child").css("display","none")
                            $("#project").val(d.project_id+"?"+d.project_name);
                            //示范应用名称
                            $("#pilot_name").val(d.pilot_name);
                            //地点单位
                            $("#place").val(d.place)
                            //时间
                            var str=d.pilot_date;
                            if(str ==null){
                                console.log("时间为null");
                            }else{
                                arr=str.split(" ");
                                var pilot_date = arr[0];
                            }
                            $("#pilot_date").val(pilot_date)
                            //内容描述
                            $("#description").val(d.description)
                        }

                    },
                    error:function(err){
                        console.log(err);
                        alert("网络故障请检查");
                    }
                });
            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    }
    //保存
    $(document).on("click","#save",function () {
        //示范应用名称
        var pilot_name=$("#pilot_name").val();
        //地点单位
        var place=$("#place").val();
        //时间
        var pilot_date=$("#pilot_date").val();
        //项目id
        var  project_id=[];
        var  project_name=[];
        $("#project").each(function () {
            if($(this).val()==""){
                var b="";
                var c="";
            }
            if($(this).val()!=""){
                var b= $(this).val().split("?")[0];
                var c= $(this).val().split("?")[1];
            }
            project_id.push(b);
            project_name.push(c);
        })
        var projectid = project_id.join('');
        var projectname=project_name.join('');
        //内容描述
        var description=$("#description").val();
        //上传文件
        formData = new FormData();
        console.log($('#imageFile')[0].files[0])
        if( $('#imageFile')[0].files[0]==undefined){
            formData.append('imageFile'," ");
        }else{
            formData.append('imageFile', $('#imageFile')[0].files[0]);
        }
        formData.append('uid', msg_data.id);
        formData.append('token', msg_data.token);
        formData.append('id', tid);
        formData.append('project_name', projectname);
        formData.append('project_id', projectid);
        formData.append('pilot_name', pilot_name);
        formData.append('place', place);
        formData.append('pilot_date', pilot_date);
        formData.append('description', description);
        $.ajax({
            type: "POST",
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
            url: "http://172.18.80.9:8087/admin/research/spPilot",
            success: function (data) {
                var data =JSON.parse(data)
                if(data.code==9){
                    alert(data.msg)
                    window.location.href="http://172.18.80.9:8087/admin/login/login"
                    return false;
                }
                if(data.code==2){
                    alert(data.msg)
                    console.log(data)
                }
                if(data.code==0){
                    alert(data.msg)
                    location.href="http://172.18.80.9:8087/admin/research/demonstrationlist"
                }


            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    })
    //提交
    $(document).on("click","#submit",function () {
        //项目id
        var  project_id=[];
        var  project_name=[];
        $("#project").each(function () {
            if($(this).val()==""){
                var b="";
                var c="";
            }
            if($(this).val()!=""){
                var b= $(this).val().split("?")[0];
                var c= $(this).val().split("?")[1];
            }
            project_id.push(b);
            project_name.push(c);
        })
        var projectid = project_id.join('');
        var projectname=project_name.join('');
        if(projectname==""){
            alert("项目名称不能为空")
            return false
        }
        //示范应用名称
        var pilot_name=$("#pilot_name").val();
        if(pilot_name==""){
            alert("示范应用名称不能为空")
            return false
        }
        //地点单位
        var place=$("#place").val();
        if(place==""){
            alert("地点不能为空")
            return false
        }
        //时间
        var pilot_date=$("#pilot_date").val();
        if(pilot_date==""){
            alert("时间不能为空")
            return false
        }
        //内容描述
        var description=$("#description").val();
        if(description==""){
            alert("内容描述不能为空")
            return false
        }
        //上传文件
        formData = new FormData();
        console.log($('#imageFile')[0].files[0])
        if( $('#imageFile')[0].files[0]==undefined){
            alert("上传文件不能为空")
            return false
        }
        formData.append('imageFile', $('#imageFile')[0].files[0]);
        formData.append('uid', msg_data.id);
        formData.append('token', msg_data.token);
        formData.append('id', tid);
        formData.append('project_name', projectname);
        formData.append('project_id', projectid);
        formData.append('pilot_name', pilot_name);
        formData.append('place', place);
        formData.append('pilot_date', pilot_date);
        formData.append('description', description);
        $.ajax({
            type: "POST",
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
            url: "http://172.18.80.9:8087/admin/research/bpPilot",
            success: function (data) {
                var data =JSON.parse(data)
                if(data.code==9){
                    alert(data.msg)
                    window.location.href="http://172.18.80.9:8087/admin/login/login"
                    return false;
                }
                if(data.code==2){
                    alert(data.msg)
                    console.log(data)
                }
                if(data.code==0){
                    if(confirm("提交后不可修改，您确定要提交吗")){
                        alert("提交成功");
                        window.location.href="http://172.18.80.9:8087/admin/research/demonstrationlist"
                    }else{
                        return false;
                    }



                }
            },
            error: function (err) {
                console.log(err);
                alert("网络故障请检查");
            }
        });
    })

})
